package com.cat.binaryTree;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/add-two-numbers/
 * @create 2025/9/27 20:33
 * @since JDK17
 */

public class Solution74 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode h = new ListNode(-1), q = h;
        int last = 0;
        while (l1 != null || l2 != null) {
            int num1 = l1 != null ? l1.val : 0;
            int num2 = l2 != null ? l2.val : 0;
            q = q.next = new ListNode((num1 + num2 + last) % 10);
            last = (num1 + num2 + last) / 10;
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null ? l2.next : null;
        }
        if (last != 0) {
            q.next = new ListNode(last);
        }
        return h.next;
    }
}
